'use strict';

const Controller = require('egg').Controller;

class MonitorController extends Controller {
  /**
   * 日志上报接口
   */
  async errorList(){
    const { ctx , service, request} = this;
    // 数据预处理
    const data = ctx.request.body;
    const {user_id,app_id,url_path, content,pageNum = 0,pageSize = 10} = data;
      // 数据保存到数据库
      const result = await ctx.service.admin.monitor.getList({
      report_type : 'error',
      user_id: user_id,
      app_id: app_id,
      url_path : url_path,
      url_path : url_path,
      content : content,
    },{pageNum, pageSize});
    ctx.success(result)
  }
  /**
   * 日志上报接口
   */
  async ajaxList(){
    const { ctx , service, request} = this;
    // 数据预处理
    const data = ctx.request.body;
    const {user_id,app_id,url_path, content,pageNum = 0,pageSize = 10} = data;
      // 数据保存到数据库
      const result = await ctx.service.admin.monitor.getList({
      report_type : 'ajax',
      user_id: user_id,
      url_path : url_path,
    },{pageNum, pageSize});
    ctx.success(result)
  }
  /**
   * 日志上报接口
   */
  async pageList(){
    const { ctx , service, request} = this;
    // 数据预处理
    const data = ctx.request.body;
    const {user_id,app_id,url_path, content,pageNum = 0,pageSize = 10} = data;
      // 数据保存到数据库
      const result = await ctx.service.admin.monitor.getList({
      report_type : 'page',
      user_id: user_id,
      app_id: app_id,
      url_path : url_path,
    },{pageNum, pageSize});
    ctx.success(result)
  }


  /**
   * 统计页面数据
   */
  async statistics(){
    const { ctx , service, request} = this;
    // 数据预处理
    const {type,key} = ctx.request.body;
    const group = {
      $group: {
        _id : `$${key}`,
        num : {
          $sum : 1
        }
      }
    }
    const sort = {
      $sort : {
        num : -1
      }
    }
    const result = await ctx.service.admin.monitor.statistics(type,[group,sort]);
    ctx.success(result)
  }
  /**
   * 获取用户具体访问路径
   */
  async visitList(){
    const {ctx} = this;
    const {mark_user,pageNum = 0,pageSize = 10} = ctx.request.body;
    const result = await ctx.service.admin.monitor.getList({
      report_type : 'page',
      mark_user
    },{pageNum, pageSize});
    ctx.success(result)
  }
}

module.exports = MonitorController;